﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace homework3
{
    public delegate void Tick();
    public delegate void Alarm(string time); 
    public class ClockEvent
    {
        
        public event Tick OnTick;
        public event Alarm OnAlarm;
        private Thread TickThread;
        private Thread AlarmThread;

        public void startTick() { 
            TickThread = new Thread(() => { OnTick(); });
            TickThread.Start();
        } 
       

        public void setAlarm(string time) {
            AlarmThread = new Thread(() => { OnAlarm(time); });
            AlarmThread.Start();
             }

        public void stop()
        {
            TickThread.Abort();
            AlarmThread.Abort();
            TickThread = null;
            AlarmThread = null;
        }
    }

    public class EventHandler 
    {
        public void Subscribe(ClockEvent clock) {
            clock.OnTick += OnTick;
            clock.OnAlarm += OnAlarm;
        }
        void OnTick() {
            while (true)
            {
                Console.WriteLine("嘀嗒");
                Thread.Sleep(1000);
            } 
        }

        void OnAlarm(string time) { 
            while (true)
            {
                string currentTime = DateTime.Now.ToString("T");
                if (currentTime.Equals(time)) {
                    Console.WriteLine("响铃,当前时间为"+time);
                    break;
                }
            }
        }
    }
}
